package com.flower8.controller;

import com.flower8.dto.CardCostRecordDto;
import com.flower8.dto.CardDto;
import com.flower8.dto.CardMonthBillDto;
import com.flower8.model.Card;
import com.flower8.service.CardService;
import com.flower8.sysconfig.constants.MsgConstants;
import com.flower8.utils.PageData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 信用卡控制层
 * @Author: tanglin
 * @Date: 2019/09/03 13:53
 * @Version: 1.0
 */
@RestController
@RequestMapping("/card")
public class CardController extends BaseController{
    private static final Logger log = LoggerFactory.getLogger(CardController.class);

    @Autowired
    private CardService cardService;

    /**
     * 查询所有信用卡列表
     * @param request
     * @return
     */
    @RequestMapping("/getCardList")
    public Object getCardList(HttpServletRequest request){
        logBefore(log, "++++++++++ 查询所有信用卡列表 ++++++++++");
        PageData pd = new PageData(request);

        pd.put("start", Integer.parseInt( pd.get("start").toString() ) );
        pd.put("length", Integer.parseInt( pd.get("length").toString() ) );

        Map res = new HashMap();
        try {
            List<CardDto> cardList =  cardService.getCardList(pd);
            int total = cardService.getCardListCount(pd);
            res.put("data", cardList);
            res.put("recordsTotal", total);   //dataTable分页需要
            res.put("recordsFiltered", total);//dataTable分页需要
            res.put("draw", pd.get("draw").toString());//dataTable分页需要
            logAfter(log);
            Map<String, Object> dataMap = (Map<String, Object>) responseResultForSuccess(res);
            // 兼容旧网页
            dataMap.put("recordsTotal", total);   //dataTable分页需要
            dataMap.put("recordsFiltered", total);//dataTable分页需要
            dataMap.put("draw", pd.get("draw").toString());//dataTable分页需要
            return dataMap;
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError(MsgConstants.ERROR_99001_MSG);
        }
    }

    /**
     *  根据用户id查询银行卡
     * @param request
     * @return
     */
    @RequestMapping("/getCardByUserId")
    public Object getCardByUserId(HttpServletRequest request){
        logBefore(log, "++++++++++  根据用户id查询银行卡 ++++++++++");
        PageData pd = new PageData(request);

        pd.put("start", Integer.parseInt( pd.get("start").toString() ) );
        pd.put("length", Integer.parseInt( pd.get("length").toString() ) );

        Map res = new HashMap();
        try {
            List<CardDto> cardList =  cardService.getCardByUserId(pd);
            int total = cardService.getCardByUserIdCount(pd);
            res.put("data", cardList);
            res.put("recordsTotal", total);   //dataTable分页需要
            res.put("recordsFiltered", total);//dataTable分页需要
            res.put("draw", pd.get("draw").toString());//dataTable分页需要
            logAfter(log);
            Map<String, Object> dataMap = (Map<String, Object>) responseResultForSuccess(res);
            // 兼容旧网页
            dataMap.put("recordsTotal", total);   //dataTable分页需要
            dataMap.put("recordsFiltered", total);//dataTable分页需要
            dataMap.put("draw", pd.get("draw").toString());//dataTable分页需要
            return dataMap;
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError(MsgConstants.ERROR_99001_MSG);
        }
    }

    /**
     * 查询信用卡消费记录
     * @param request
     * @return
     */
    @RequestMapping("/getCardCostRecord")
    public Object getCardCostRecord(HttpServletRequest request){
        logBefore(log, "++++++++++  查询信用卡消费记录 ++++++++++");
        PageData pd = new PageData(request);

        pd.put("start", Integer.parseInt( pd.get("start").toString() ) );
        pd.put("length", Integer.parseInt( pd.get("length").toString() ) );

        Map res = new HashMap();
        try {
            List<CardCostRecordDto> cardList =  cardService.getCardCostRecord(pd);
            int total = cardService.getCardCostRecordCount(pd);
            res.put("data", cardList);
            res.put("recordsTotal", total);   //dataTable分页需要
            res.put("recordsFiltered", total);//dataTable分页需要
            res.put("draw", pd.get("draw").toString());//dataTable分页需要
            logAfter(log);
            Map<String, Object> dataMap = (Map<String, Object>) responseResultForSuccess(res);
            // 兼容旧网页
            dataMap.put("recordsTotal", total);   //dataTable分页需要
            dataMap.put("recordsFiltered", total);//dataTable分页需要
            dataMap.put("draw", pd.get("draw").toString());//dataTable分页需要
            return dataMap;
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError(MsgConstants.ERROR_99001_MSG);
        }
    }

    /**
     * 查询信用卡月度账单记录
     * @param request
     * @return
     */
    @RequestMapping("/getCardMonthRecord")
    public Object getCardMonthRecord(HttpServletRequest request){
        logBefore(log, "++++++++++  查询信用卡月度账单记录 ++++++++++");
        PageData pd = new PageData(request);

        pd.put("start", Integer.parseInt( pd.get("start").toString() ) );
        pd.put("length", Integer.parseInt( pd.get("length").toString() ) );

        Map res = new HashMap();
        try {
            List<CardMonthBillDto> cardList =  cardService.getCardMonthRecord(pd);
            int total = cardService.getCardMonthRecordCount(pd);
            res.put("data", cardList);
            res.put("recordsTotal", total);   //dataTable分页需要
            res.put("recordsFiltered", total);//dataTable分页需要
            res.put("draw", pd.get("draw").toString());//dataTable分页需要
            logAfter(log);
            Map<String, Object> dataMap = (Map<String, Object>) responseResultForSuccess(res);
            // 兼容旧网页
            dataMap.put("recordsTotal", total);   //dataTable分页需要
            dataMap.put("recordsFiltered", total);//dataTable分页需要
            dataMap.put("draw", pd.get("draw").toString());//dataTable分页需要
            return dataMap;
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError(MsgConstants.ERROR_99001_MSG);
        }
    }
}
